package cn.myfirefly.moming.house.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.controller.BaseCommonController;
import cn.myfirefly.moming.common.model.StaffDto;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeContractFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.moming.house.service.SpfTradeWageService;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("spfTradeContract")
public class SpfTradeContractController extends BaseCommonController {

	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@Autowired
	private SpfTradeWageService spfTradeWageService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "spftradecontract/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontract/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontract/add";
	}
	
	@RequestMapping("show.do")
	public String show(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funcId", "spfTradeContract");
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontract/show";
	}
	
	@RequestMapping("wagelist.do")
	@ResponseBody
	public PageResultDto<SpfTradeWageDto> wagelist(SpfTradeWageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return new PageResultDto<SpfTradeWageDto>();
		}
		dto.setTradeContractId(new Integer(contractId));
		PageResultDto<SpfTradeWageDto> result = spfTradeWageService.getList(dto);
		return result;
	}
	
	@RequestMapping("addSave.do")
	@ResponseBody
	public ResultDto addSave(SpfTradeContractDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() != null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request, true);
		dto.setTradeContractStatus("1");
		spfTradeContractService.insert(dto);
		return success("保存成功");
	}
	
	@RequestMapping("editSave.do")
	@ResponseBody
	public ResultDto editSave(SpfTradeContractDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() == null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request, false);
		int count = spfTradeContractService.update(dto);
		if (count == -1) {
			return error("修改的数据不存在，请刷新后再试");
		}
		if (count == -2) {
			return error("当前数据状态为已完成或者已取消，不能修改");
		}
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeContractDto> list(SpfTradeContractFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		this.wrapDataAuth(dto);
		PageResultDto<SpfTradeContractDto> result = spfTradeContractService.getList(dto);
		return result;
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getSpfTradeContract(SpfTradeContractDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeContractDto spfTradeContract = spfTradeContractService.getSpfTradeContract(dto.getId());
		if (!StrUtil.equals(spfTradeContract.getCreateBy(), this.getLoginUser(request).getUserName())) {
			return error("当前数据不属于您，不能编辑");
		}
		return success(spfTradeContract);
	}
	
	@RequestMapping("getDetail.do")
	@ResponseBody
	public ResultDto getContractDetail(HttpServletRequest request, 
			HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StrUtil.isBlank(id)) {
			return error("请求参数错误");
		}
		SpfTradeContractDetailDto dto = spfTradeContractService.getContractDetail(new Integer(id));
		return success(dto);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(SpfTradeContractDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = spfTradeContractService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("toDepartmentLookUp.do")
	public String toDepartmentLookUp(HttpServletRequest request, 
			HttpServletResponse response) {
		return super.departmentLookUp(request, response);
	}
	
	@RequestMapping("staffLookup.do")
	public String toStaffLookup(StaffDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		return super.staffLookup("spfTradeContract", dto, request, response);
	}
}
